package com.linux.huhx.referenceTest;

import com.linux.huhx.beanwrap.Person;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

/**
 * Created by huhx on 2017-06-02.
 */
public class ReferenceTest {
    public static void main(String[] args) {
        SoftReference<Person> softReference = new SoftReference<Person>(new Person("huhx", "123456"));
        System.out.println(softReference.get()); // username: huhx, password: 123456
        System.gc();
        System.runFinalization();
        System.out.println(softReference.get()); // username: huhx, password: 123456

        WeakReference<Person> weakReference = new WeakReference<Person>(new Person("linux", "12345"));
        System.out.println(weakReference.get()); // username: linux, password: 12345
        System.gc();
        System.runFinalization();
        System.out.println(weakReference.get()); // null

        ReferenceQueue<String> refQueue = new ReferenceQueue<String>();
        PhantomReference<String> referent = new PhantomReference<String>(new String("T"), refQueue);
        System.out.println(referent.get());// null
        System.gc();
        System.runFinalization();
        System.out.println(refQueue.poll() == referent); // true
    }
}
